import { defineStore } from 'pinia'
import { getUrlWithParams } from '@/utils/utils'
import { setStorage, getStorage } from '@/app/storage'
import { TABS_VIEW } from '@/const/storage'
import { TABS_VIEW_STORE_NAME } from '../const'
import router from '@/router'
import useKeepAliveStore from './keep-alive'

export default defineStore(TABS_VIEW_STORE_NAME, () => {
  const keepAliveStore = useKeepAliveStore()

  const tabsList = ref(getStorage(TABS_VIEW) || [])

  // 添加tabs
  function addTabs(route) {
    if (tabsList.value.every((item) => item.fullPath !== route.fullPath)) {
      tabsList.value.push(route)
    }
    if (
      keepAliveStore.keepAliveList.includes(route.name) &&
      route.isKeepAlive
    ) {
      keepAliveStore.addKeepAlive(route.name)
    }

    setStorage(TABS_VIEW, tabsList.value)
  }

  // 关闭当前
  function closeTab(fullPath, isCurrent = true) {
    if (isCurrent) {
      tabsList.value.forEach((item, index) => {
        if (item.fullPath !== fullPath) return
        const nextTab = tabsList.value[index + 1] || tabsList.value[index - 1]
        if (!nextTab) return
        router.push({ path: nextTab.fullPath })
      })
    }
    const tabs = tabsList.value
    const item = tabs.find((item) => item.path === fullPath)
    item?.isKeepAlive && keepAliveStore.removeKeepAlive(item.name)
    const tmpTabs = tabs.filter((item) => item.fullPath !== fullPath)
    setStorage(TABS_VIEW, tmpTabs)
    tabsList.value = tmpTabs
  }

  // 关闭左/右
  function closeLeftRightTabs(fullPath, type) {
    const index = tabsList.value.findIndex((item) => item.fullPath === fullPath)
    if (index !== -1) {
      const range =
        type === 'left' ? [0, index] : [index + 1, tabsList.value.length]
      tabsList.value = tabsList.value.filter(
        (item, index) => index < range[0] || index >= range[1] || !item.close,
      )
    }

    const tabs = tabsList.value
    const keepAliveList = tabs.filter((item) => item.isKeepAlive)
    keepAliveStore.setKeepAlive(keepAliveList.map((item) => item.fullPath))
    setStorage(TABS_VIEW, tabs)
  }

  // 关闭其他
  function closeOtherTabs(fullPath) {
    const tabs = tabsList.value.filter(
      (item) => item.fullPath === fullPath || !item.close,
    )
    setStorage(TABS_VIEW, tabs)
    tabsList.value = tabs
    const keepAliveList = tabs.filter((item) => item.isKeepAlive)
    keepAliveStore.setKeepAlive(keepAliveList.map((item) => item.name))
  }

  // 关闭所有
  function closeAllTabs() {
    const tabs = tabsList.value.filter((item) => item.isAffix)
    tabsList.value = tabs
    keepAliveStore.setKeepAlive(tabs.filter((item) => item.isKeepAlive))
    setStorage(TABS_VIEW, tabs)
  }

  // 更新tabs
  function updateTabs(route) {
    tabsList.value.forEach((item) => {
      if (item.fullPath === route.fullPath) {
        item = Object.assign(item, route)
      }
    })

    setStorage(TABS_VIEW, tabsList.value)
  }

  // 更新tab title
  function updateTabTitle(title = '') {
    tabsList.value.forEach((item) => {
      if (item.fullPath === getUrlWithParams()) {
        item.meta.title = title
      }
    })

    setStorage(TABS_VIEW, tabsList.value)
  }

  return {
    tabsList,
    addTabs,
    closeTab,
    closeLeftRightTabs,
    closeOtherTabs,
    closeAllTabs,
    updateTabs,
    updateTabTitle,
  }
})
